package com.yt.app.core.utils;

import com.yt.util.MD5Util;
import org.springframework.util.Base64Utils;

public class TokenUtil {
    public static String makeToken(Long uid,  String password, Short userType){
        StringBuilder sb = new StringBuilder().append(uid).append(":").append(password).append(":").append(userType);
        String token = Base64Utils.encodeToUrlSafeString((uid + ":" + MD5Util.MD5(sb.toString())).getBytes());
        return token;
    }

    public static Long getUIDFromToken(String token){
        try {
            String t = new String(Base64Utils.decodeFromUrlSafeString(token));
            String[] a = t.split(":");
            return Long.parseLong(a[0]);
        }catch (Exception ex){
            return null;
        }
    }
}
